<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/7/23
 * Time: 9:12
 */

namespace app\alliance\controller;


use app\service\BusinessService;
use app\service\jobService;
use app\service\MemberService;
use app\service\ResourceService;
use Redis\Redis;
use think\App;
use think\Db;

class Business extends Common
{

    public $BusinessService;

    function __construct(App $app = null)
    {
        parent::__construct($app);
        $this->BusinessService = new BusinessService();
    }

    /**
     * @return array|bool
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * @context 玩家生意列表
     */
    function index()
    {
        return $this->BusinessService->getsMemberBusiness();
    }

    /**
     * @return array|null|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @context 生意详情
     */
    function detail()
    {
        $business_id = input("post.business_id");
        return $this->BusinessService->getsBusinessDetail($business_id);
    }

    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * @context 超级玩家上市 重新开始 返回首页
     */
    function superImmediatelyOnMark()
    {
        $mark = input("post.mark"); // 上市 重新开始传1 返回首页传2
        $super_id = input("post.super_id");   // 原来的超级玩家生意id 上市不需要传
        return BusinessService::immediatelyOnMark($mark, $super_id);
    }

    /**
     * @return bool
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     * @context 摊位交租金
     */
    function payRent()
    {
        $mb_id = input("post.id");// 用户生意id
        $grid = input("post.grid");   // 用户交租摊位
        $day_price = input("post.day_price"); // 日租金
        return $this->BusinessService->payRent($mb_id, $grid, $day_price);
    }

    /**
     * @return array
     * @throws \think\Exception
     * @context 用户生意恢复
     */
    function recoverBussiness()
    {
        $mb_id = input("post.id"); // 用户生意id
        $grid = input("post.grid");   // 用户恢复摊位
        $perioy = input("post.perioy");  // 用户恢复周期（多少天）
        $super_player = input("post.super_player");
        $need = input("post.need");
        return $this->BusinessService->recoverBussiness($mb_id, $grid, $perioy, $super_player, $need);

    }

    /**
     * @return mixed
     * @context 获取可选择的价值区间
     */
    function chooseSection()
    {
        return array("status" => true, "data" => $this->BusinessService->getCanUseValueSection($this->level));
    }

    /**
     * @return mixed
     * @context 获取等级可参与匹配价值金额
     */
    function levelAllSection()
    {
        return $this->BusinessService->getMatchValueSections($this->level - 1);
    }

    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     * @context 用户参与匹配
     */
    function startMatching()
    {

        $grid = input("post.grid"); // 用户匹配的栏位
        $data = input("post.data");   // 用户选择的匹配价值区间
        $value_section = array("value_section" => $data);
        return $this->BusinessService->participationMatching($grid, $value_section);
    }

    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     * @context 生意出售
     */
    function saleMemberBusiness()
    {
        $id = input("post.id");
        $grid = input("post.grid");
        $data = Db::table("member_business")->where(array("id" => $id))->find();
        $mark = input("post.mark");
        return $this->BusinessService->saleBusiness($id, $grid, $data, $mark);
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @context 选择匹配的生意列表
     */
    function chooseMemberBusiness()
    {
        $grid = input("post.grid");
        return $this->BusinessService->chooseBusiness($grid);
    }

    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * @context 交易信息查看
     */
    function tradeInformations()
    {
        $sale_member_code = input("post.sale_member_code"); // 卖方code
        $id = input("post.id");   // 卖方生意id
        //$data=Db::table("member_business")->where(array("id"=>$id))->find();
        $data = json_decode(input("post.data"), true);
        return $this->BusinessService->tradeInformation($id, $sale_member_code, $data);
    }

    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     * @context 交易信息提交
     */
    function confirmTradeBusiness()
    {
        $id = input("post.id");
        $data = json_decode(input("post.data"), true);
        $data['pract_price'] = input("post.pract_price");
        $data['screenshot'] = input("post.screenshot");
        $data['amount'] = input("post.amount");
        $data['voucher_id'] = input("post.voucher_id");
        $data['voucher_price'] = input("post.voucher_price");
        $data['voucher_title'] = input("post.voucher_title");
        $data['pay_type'] = input("post.pay_type");

        # 金币支付
        $pwd = input('post.pwd');
        # 判断金币是否足够
        if ($data['pract_price'] > ($this->memberInfo['gold']+$this->memberInfo['bind_gold'])) {
            return array('status' => false, 'msg' => '金币不足');
        }
        # 判断密码是否正确
        if (md5($pwd) != $this->memberInfo['operate_pwd']) {
            return array('status' => false, 'msg' => '操作密码不正确');
        }
        # 扣除金币
        if($this->memberInfo['bind_gold']>=$data['pract_price']){
            Db::table("member_property")->where(array("member_code" => $this->code))->setDec('bind_gold', $data['pract_price']);
            # 减少金币
            $this->memberInfo['gold'] -= $data['pract_price'];
        }else{
            Db::table("member_property")->where(array("member_code" => $this->code))->update(array('bind_gold'=>0));
            Db::table("member_property")->where(array("member_code" => $this->code))->setDec('gold', ($data['pract_price']-$this->memberInfo['bind_gold']));
            $this->memberInfo['gold']-=$data['pract_price']-$this->memberInfo['bind_gold'];
            $this->memberInfo['bind_gold'] = 0;
        }
        # 金币记录
        MemberService::addLog_gold($this->code, $data['pract_price'], 21);
        # 更新redis
        $redis = new Redis();
        $redis->hSetJson('memberInfo', $this->code, $this->memberInfo);
        return $this->BusinessService->confirmTrade($data);
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @context 卖家确认交易查看信息
     */
    function checkSuccessInformation()
    {
        $id = input("post.trad_audit_id"); // 交易审核id
            return $this->BusinessService->successInformation($id);
    }

    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * @context 卖家确认交易
     */
    function successTradeConfirm()
    {
        $id = input("post.trad_audit_id"); // 交易审核id
        $data = Db::table("trad_audit")->where(array("id" => $id))->find();
        $data['sell_id'] = $data['industry_id'];
        return $this->BusinessService->successTrade($data);
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @context 获取对方信息
     */
    function checkTradeInfomation()
    {
        $trad_audit_id = input("post.trad_audit_id");
        $main = input("post.main");
        return $this->BusinessService->checkTradeInfomation($trad_audit_id, $main);
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @context 等待买家付款 查看信息
     */
    function waitBuyerPayment()
    {
        $buy_code = input("post.buy_code");
        return $this->BusinessService->waitBuyerPayment($buy_code);
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @context 等待付款
     */
    function waitPayment()
    {
        $member_business_id = input("post.member_business_id");
        return $this->BusinessService->waitPayment($member_business_id);
    }

    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * @context 玩家交易投诉
     */
    function sellComplaintReporting()
    {
        $id = input("post.trad_audit_id"); // 交易审核id
        $data = Db::table("trad_audit")->field("id,buy_code,buy_name,buy_phone,industry_id,
        sell_code,sell_name,sell_phone")->where(array("id" => $id))->find();
        $screenshot = json_decode(input("post.screenshot"), true);
        $_base_url = array();
        if (!empty($screenshot)) {
            foreach ($screenshot as $k => $v) {
                array_push($_base_url, ResourceService::uploadBase($v));
            }
        }
        $data['screenshot'] = implode(',', $_base_url);// 投诉图片逗号拼接
        $data['complaint_context'] = input("post.complaint_context");
        $data['reason'] = input("post.reason");
//        $data['trad_perioy'] = input("post.trad_perioy");
        return $this->BusinessService->complaintReporting($data);
    }

    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * @context 重新出售
     */
    function tradAgains()
    {
        $id = input("post.id");
        $data = Db::table("member_business")->where(array("id" => $id))->find();
        $data['sell_grid'] = input("post.grid");
        return $this->BusinessService->tradAgain($data);
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @context 仓库列表
     */
    function getWareHouse()
    {
        return $this->BusinessService->wareHouseList();
    }

    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     * @context 从栏位移动到仓库
     */
    function startMoveToWare()
    {
        $grid = input("post.grid");   // 用户栏位id
        $business_id = input("post.id"); // 用户生意的id
        return $this->BusinessService->moveToWare($grid, $business_id);
    }

    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * @context 从仓库移动到栏位
     */
    function startShiftToWare()
    {
        $data = array(input("post.data"));
        return $this->BusinessService->shiftToWare($data);
    }

    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     * @context 仓库批量出售生意
     */
    function batchSaleBusiness()
    {
        $data = array(input("post.data"));
        return $this->BusinessService->batchSaleBusiness($data);
    }

    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * @context 玩家生意升级
     */
    function upgradeBusiness()
    {
        $data = input("post.data");
        $_param = array($data);
        return $this->BusinessService->upgradeBusiness($_param);
    }

    /**
     * @param $data
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * @context 玩家生意分解
     */
    function resolveBusiness()
    {
        $data = array(input("post.data"));
        return $this->BusinessService->resolveBusiness($data);
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @context 查询下个等级的生意id
     */
    function checkNextLevel()
    {
        $currentLevel = input("post.currentLevel");
        return $this->BusinessService->checkNextLevel($currentLevel);
    }

    /**
     * @return array
     * @context 玩家生意列表
     */
    function myBusinessList()
    {
        return $this->BusinessService->myBusinessList();
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @context 获取玩家恢复状态信息
     */
    function getsMemberRecoverInfo()
    {
        return $this->BusinessService->getsMemberRecoverInfo();
    }

    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * @context 用户恢复账号冻结状态
     */
    function memberRecover()
    {
        $need = input("post.need");
        return $this->BusinessService->memberRecover($need);
    }

    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * @context 刷新用户单个栏位
     */
    function reflushMemberGrid()
    {
        $grid = input("post.grid");
        return $this->BusinessService->reflushMemberGrid($grid);
    }

    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     * @context 用户取消匹配
     */
    function cancelMatch()
    {
        $grid = input("post.grid");
        $jobservice = new jobService();
        return $jobservice->cancelMatching($this->code, $grid);
    }

    /**
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     * @context 重新匹配
     */
    function reMatch()
    {
        $grid=input("post.grid");
        if(!empty($grid)){
            $businessService=new BusinessService();
            $businessService->anewMatch($this->code,$grid);
        }
        return array("status"=>true);
    }

    /**
     * @return array
     * @context 生意是否可以继续购买
     */
    function canPurchase()
    {
        $id=input("post.id");
        $businessService=new BusinessService();
        return $businessService->canPurchaseService($id);
    }
}